package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.NewsType;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.mapper.NewsTypeMapper;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsTypeMapper newsTypeMapper;
    @Autowired
    private NewsContentMapper newsContentMapper;

    /**
     * 骡窝日报页面
     *
     * @param qo
     * @return
     */
    public PageInfo queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(newsMapper.queryForList(qo));
    }

    /**
     * 更改日报状态
     *
     * @param news
     */
    public void changeState(News news) {
        if ((news.getState() == News.NEWS_RELEASE) || (news.getState() == News.NEWS_HOT)) {
            news.setReleaseTime(new Date());
            news.setRelease(true);
        }
        newsMapper.changeState(news);
    }

    /**
     * 删除日报
     *
     * @param id
     */
    public void delete(Long id) {
        newsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 获取所有日报类型
     *
     * @return
     */
    public List<NewsType> typeList() {
        return newsTypeMapper.selectAll();
    }

    /**
     * 获取日报内容
     *
     * @param id
     * @return
     */
    public NewsContent getContentById(Long id) {
        return newsContentMapper.selectByPrimaryKey(id);
    }

    /**
     * 保存骡窝日报
     *
     * @param news
     */
    public void save(News news) {
        news.setCreateTime(new Date());
        newsMapper.insert(news);
        NewsContent content = news.getDetails();
        content.setId(news.getId());
        newsContentMapper.insert(content);
    }

    /**
     * 更新骡窝日报
     *
     * @param news
     */
    public void update(News news) {
        newsMapper.updateByPrimaryKey(news);
        NewsContent content = news.getDetails();
        content.setId(news.getId());
        newsContentMapper.updateByPrimaryKey(content);
    }

    /**
     * 获取首页日报
     * @param qo
     * @return
     */
    public PageInfo queryForHot(NewsQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo(newsMapper.queryForHot(qo));
    }

    /**
     * 根据id获取日报内容
     * @param id
     * @return
     */
    public News getById(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }
}
